﻿using System;
using System.Web.Mvc;
using Localization;
using SinBiota.Model.Business.PersistenceServices.Interfaces;
using SinBiota.Model.Entities;
using Sinbiota.Web.ActionFilters;
using System.Collections.Generic;

namespace Sinbiota.Web.Controllers
{
    
    [AdminPermission]
    public class MicroHabitatController : BaseController
    {
        private readonly IMicroHabitatPersistenceServices _microHabitatPersistenceServices;

        public MicroHabitatController(IMicroHabitatPersistenceServices microHabitatPersistenceServices)
        {
            _microHabitatPersistenceServices = microHabitatPersistenceServices;
        }

        //
        // GET: /MicroHabitat/
        [ImportMessagesFromTempData]
        public ActionResult Index()
        {
            return View(_microHabitatPersistenceServices.FindAll());
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            return View(_microHabitatPersistenceServices.FindByNameLike(collection[SearchBoxName]));
        }


        //
        // GET: /MicroHabitat/Details/5
        [ImportMessagesFromTempData]
        public ActionResult Details(int id)
        {
            var microHabitat = _microHabitatPersistenceServices.FindById(id);
            return microHabitat == null ? View("NotFound") : View(microHabitat);
        }

        //
        // GET: /MicroHabitat/Create
        [ImportMessagesFromTempData]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /MicroHabitat/Create
        [HttpPost, ExportMessagesToTempData]
        public ActionResult Create(MicroHabitat microHabitat)
        {
            var newMicroHabitat = new MicroHabitat();
            TryUpdateModel(newMicroHabitat);
            if (ModelState.IsValid)
            {
                _microHabitatPersistenceServices.Save(newMicroHabitat);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(microHabitat);
        }

        //
        // GET: /MicroHabitat/Edit/5
        [ImportMessagesFromTempData]
        public ActionResult Edit(int id)
        {
            var microHabitat = _microHabitatPersistenceServices.FindById(id);
            return microHabitat == null ? View("NotFound") : View(microHabitat);
        }

        //
        // POST: /MicroHabitat/Edit/5
        [HttpPost, ExportMessagesToTempData]
        public ActionResult Edit(int id, MicroHabitat microHabitat)
        {
            var editMicroHabitat = _microHabitatPersistenceServices.FindById(id);
            TryUpdateModel(editMicroHabitat);
            if (ModelState.IsValid)
            {
                _microHabitatPersistenceServices.Update(editMicroHabitat);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(microHabitat);
        }

        //
        // GET: /MicroHabitat/Delete/5
        [ImportMessagesFromTempData]
        public ActionResult Delete(int id)
        {
            var microHabitat = _microHabitatPersistenceServices.FindById(id);
            return microHabitat == null ? View("NotFound") : View(microHabitat);
        }

        //
        // POST: /MicroHabitat/Delete/5
        [HttpPost, ExportMessagesToTempData]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var deleteMicroHabitat = _microHabitatPersistenceServices.FindById(id);
            if (deleteMicroHabitat == null) return View("NotFound");
            _microHabitatPersistenceServices.Delete(deleteMicroHabitat);
            SetInformationMessage(Messages.OperationCompletedSuccessfully);
            return RedirectToAction("Index");
        }
    }
}
